package com.shiwei.is.transmission;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.shiwei.is.tools.Helper;
import com.shiwei.is.tools.OkHttpUtil;
import com.shiwei.is.view.IImageSender;

import java.util.HashMap;

import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;

/**
 * File: null.java
 * Creator: dingpwen
 * Company: shiwei
 * Created on: 2021/3/25 12:54
 * Email: dingpwen@gmail.com
 * Details: TODO
 */
public class ImageSender implements IImageSender {
    private SenderThread sender;
    private final Context mContext;
    private SendListener mListener;

    public ImageSender(Context context) {
        mContext = context;
    }

    @Override
    public void startSend() {
        sender = new SenderThread();
        sender.start();
        //sender.setTestWrite(new TestWrite(mContext));
        startWebSocket();
    }

    @Override
    public void endSend() {
        if(sender != null) {
            sender.exit();
            sender = null;
        }
    }

    @Override
    public void sendDataToRemote(byte[] videoBuffer, int size) {
        //Log.d("ISApp:ImageSender", "size = " + size);
        if(sender != null) {
            sender.send(videoBuffer, size);
        }
    }

    public void reStartWebSocket() {
        if(mListener != null) {
            mListener.closeWebSocket();
            mListener = null;
        }
        startWebSocket();
    }

    private void startWebSocket() {
        mListener = new SendListener();
        String token = Helper.getUserToken(mContext, true);
        HashMap<String, String> map = new HashMap<>();
        map.put(Helper.WS_MSG_TOKEN_SELF, token);
        OkHttpUtil.createWebSocket(Helper.getWebSocketUrl(mContext), mListener, map);
    }

    final class SendListener extends WebSocketListener {
        private WebSocket mWebSocket;
        private boolean mOpened = false;
        @Override
        public void onOpen(@NonNull WebSocket webSocket, @NonNull Response response) {
            super.onOpen(webSocket, response);
            mWebSocket = webSocket;
            mOpened = true;
            if(sender != null) {
                sender.setWebSocket(webSocket);
            }
        }

        @Override
        public void onClosed(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
            super.onClosed(webSocket, code, reason);
            mOpened = false;
            if(sender != null) {
                sender.setWebSocket(null);
            }
        }

        @Override
        public void onFailure(@NonNull WebSocket webSocket, @NonNull Throwable t, Response response) {
            super.onFailure(webSocket, t, response);
            //DebugLog.warn("onFailure:" + t);
            Log.d("ISApp:ImageSender", "onFailure:" + t);
        }

        public void closeWebSocket() {
            if(mOpened) {
                mWebSocket.close(1000, "Normal");//RFC 6455
            }
        }
    }
}
